package com.funtl.framework.java.common.util.calculation;

/**
 * 经纬度通用工具
 * @author lwm
 *
 */
public class Location {
	private static final double EARTH_RADIUS = 6378137;
	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}
	
	/**
	 * 根据经纬度计算两点之间的距离
	 * @param lonA A点经度
	 * @param latA A点纬度
	 * @param lonB B点经度
	 * @param latB B点纬度
	 * @return 米
	 */
	public static long getDistance(double lonA, double latA, double lonB, double latB) {
		double radLat1 = rad(latA);
		double radLat2 = rad(latB);
		double a = radLat1 - radLat2;
		double b = rad(lonA) - rad(lonB);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		return new Double(s).longValue();
	}
	
}
